<template>
  <div class="app-container">
    <!--条件搜索表单-->
    <div style="margin-bottom: -20px">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="searchObj.paramName" placeholder="参数名称" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="searchObj.paramCode" placeholder="参数编码" />
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchObj.paramType" placeholder="参数类型" clearable @change="getAll()">
            <el-option v-for="item in paramTypeOptions" :key="item.value" :label="item.text" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getAll()">查询</el-button>
          <el-button icon="el-icon-refresh" @click="reset()">清空</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 工具栏 -->
    <div class="el-toolbar" style="margin-top: 10px">
      <div class="el-toolbar-body">
        <el-button v-if="hasPerm('sys:param:save')" type="primary" icon="el-icon-plus" @click="openEdit(null)">新增</el-button>
        <el-button v-if="hasPerm('sys:param:batch-delete')" type="danger" icon="el-icon-delete" :disabled="ids.length < 1" @click="batchDelete">
          批量删除
        </el-button>
      </div>
    </div>

    <!-- banner列表 :data="list"表示遍历list数组 stripe条纹 -->
    <el-table
      v-loading="loading"
      element-loading-text="正在努力加载数据中..."
      :data="page.records"
      :border="true"
      stripe
      style="width: 100%; margin: 15px 0px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" fixed="left" width="55" />
      <el-table-column type="index" fixed="left" width="80" label="序号" />
      <el-table-column prop="paramName" fixed="left" label="参数名称" min-width="120" sortable show-overflow-tooltip />
      <el-table-column prop="paramCode" label="参数编码" min-width="120" sortable show-overflow-tooltip />
      <el-table-column prop="paramValue" label="参数值" min-width="120" sortable show-overflow-tooltip />
      <el-table-column prop="paramType" label="参数类型" min-width="120" sortable :filters="paramTypeOptions" :filter-method="filterHandler">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.paramType == 0" type="primary">系统</el-tag>
          <el-tag v-if="scope.row.paramType == 1" type="primary">业务</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="priority" label="优先级" min-width="90" sortable />
      <el-table-column prop="status" label="状态" min-width="100" sortable :filters="statusOptions" :filter-method="filterHandler">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" :active-value="0" :inactive-value="1" @change="handleStatusChange(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" show-overflow-tooltip />
      <el-table-column prop="createTime" label="创建时间" min-width="120" sortable show-overflow-tooltip />

      <!-- 操作 -->
      <el-table-column label="操作" width="150" fixed="right" align="center">
        <template slot-scope="scope">
          <el-link type="success" icon="el-icon-edit" @click="openEdit(scope.row)">编辑</el-link>
          <el-divider direction="vertical" />
          <el-link type="danger" icon="el-icon-remove" @click="remove(scope.row.id)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页导航栏 -->
    <div>
      <el-pagination
        :background="true"
        :current-page="pIndex"
        :page-size="pSize"
        :total="page.total"
        :page-sizes="[1, 3, 6, 9, 12, 15, 18]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        @current-change="getAll"
        @size-change="handleSizeChange"
      />
    </div>

    <!-- 表单 -->
    <el-dialog :title="dialogTitle" :visible.sync="visible" @close="close('paramForm')">
      <el-form ref="paramForm" :model="param" :rules="rules" label-width="80px">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="ID" prop="id" :hidden="true">
              <el-input v-model="param.id" disabled />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="参数名称" prop="paramName">
              <el-input v-model="param.paramName" placeholder="请输入参数名称" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="参数编码" prop="paramCode">
              <el-input v-model="param.paramCode" placeholder="请输入参数编码" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="参数值" prop="paramValue" required>
              <el-input v-model="param.paramValue" placeholder="请输入参数值" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="参数类型" prop="paramType">
              <el-select v-model="param.paramType" style="width: 100%">
                <el-option v-for="item in paramTypeOptions" :key="item.value" :label="item.text" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="状态" prop="status">
              <el-select v-model="param.status" style="width: 100%">
                <el-option v-for="item in statusOptions" :key="item.value" :label="item.text" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="优先级" prop="priority">
              <el-input-number v-model="param.priority" controls-position="right" placeholder="请输入优先级" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="参数备注" prop="remark">
              <el-input v-model="param.remark" :rows="3" type="textarea" placeholder="请输入备注" maxlength="255" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close('paramForm')">取 消</el-button>
        <el-button type="primary" @click="save('paramForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 引入api接口定义的js文件
import paramApi from '@/api/sys/param'

export default {
  data() {
    // 定义变量和初始值
    return {
      paramTypeOptions: [
        { value: 0, text: '系统参数' },
        { value: 1, text: '业务参数' }
      ],
      statusOptions: [
        { value: 0, text: '正常' },
        { value: 1, text: '停用' }
      ],
      loading: false, // 加载状态
      pIndex: 1, // 当前页
      pSize: 6, // 每页显示记录数
      searchObj: {}, // 条件封装对象
      page: [], // 列表
      visible: false, // 表单显示
      ids: [],

      // 表单
      dialogTitle: '',
      param: {
        id: undefined,
        paramName: '',
        paramCode: '',
        paramValue: '',
        paramType: 1,
        status: 0,
        priority: 0,
        remark: ''
      },

      // 验证规则
      rules: {
        paramName: [{ required: true, message: '参数名称不能为空', trigger: 'blur' }],
        paramCode: [
          { required: true, message: '参数编码不能为空', trigger: 'blur' },
          { pattern: /^[A-Za-z_]+$/, message: '参数编码只能包含字母和下划线', trigger: 'blur' }
        ],
        paramValue: [{ required: true, message: '参数值不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getAll()
  },
  methods: {
    // 加载列表数据
    getAll(current = 1) {
      this.loading = true
      this.pIndex = current
      paramApi
        .getPage(this.pIndex, this.pSize, this.searchObj)
        .then((response) => {
          this.page = response.data
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 行内筛选
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },

    // 复选框选中
    handleSelectionChange(r) {
      // 得到选中的id
      this.ids = r.map((v) => v.id)
    },

    // 改变页码
    handleSizeChange(pageSize) {
      this.pSize = pageSize
      // 重新调用数据加载方法 --> 方法中已经加入的查询参数
      this.getAll()
    },

    // 改变状态
    handleStatusChange(row) {
      const text = row.status === 0 ? '正常' : '停用'
      this.$confirm(`此操作将修改 '${row.paramName}' 状态为 '${text}', 是否继续?`, '参数状态修改', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          paramApi
            .updateStatus(row.id, row.status)
            .then((resp) => {
              this.$message.success('操作成功！')
            })
            .catch(() => {
              row.status = row.status === 0 ? 1 : 0
            })
        })
        .catch(() => {
          row.status = row.status === 0 ? 1 : 0
        })
    },

    // 删除
    remove(id) {
      this.$confirm('此操作将永久删除参数信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        paramApi.deleteById(id).then((resp) => {
          this.$message.success('删除成功!')
          this.getAll()
        })
      })
    },

    // 打开编辑
    openEdit(row) {
      this.dialogTitle = '新增参数'
      this.visible = true
      this.param.priority = this.page.total + 2

      if (row != null) {
        this.dialogTitle = '编辑参数'
        paramApi.getById(row.id).then((res) => {
          Object.assign(this.param, res.data)
        })
      }
    },

    // 关闭表单
    close(form) {
      this.visible = false
      this.$refs[form].resetFields()
    },

    // 保存
    save(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const apiCall = this.param.id ? paramApi.update(this.param) : paramApi.save(this.param)
          apiCall.then(() => {
            this.$message.success('操作成功')
            this.close(form)
            this.getAll()
          })
        }
      })
    },

    // 批量删除
    batchDelete() {
      if (this.ids.length > 0) {
        this.$confirm('此操作将永久删除参数信息,是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          // 确定执行then方法
          .then(() => {
            paramApi.batchDelete(this.ids).then((resp) => {
              this.$message.success('删除成功！')
              this.getAll()
            })
          })
          .catch(() => {
            this.$message.info('已取消删除！')
          })
      } else {
        this.$message.warning('至少选择一条数据!')
      }
    },

    // 重置
    reset() {
      this.searchObj = {}
      this.getAll()
    }
  }
}
</script>

<style></style>
